#!/bin/bash

# Houzz爬虫Web服务启动脚本

echo "🚀 启动Houzz爬虫Web服务..."
echo "=================================="

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "❌ 未找到Python3，请先安装Python"
    exit 1
fi

# 检查依赖
echo "📦 检查依赖包..."
python3 -c "import flask, pandas, redis, requests, lxml" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "❌ 缺少依赖包，正在安装..."
    pip install -r requirements.txt
    if [ $? -ne 0 ]; then
        echo "❌ 依赖安装失败"
        exit 1
    fi
fi

# 创建必要目录
echo "📁 创建目录..."
mkdir -p exports logs/houzz templates

# 检查Redis连接
echo "🔗 检查Redis连接..."
python3 -c "
import redis
try:
    r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
    r.ping()
    print('✅ Redis连接正常')
except:
    print('❌ Redis连接失败，请确保Redis服务正在运行')
    exit(1)
"

if [ $? -ne 0 ]; then
    echo "请先启动Redis服务: redis-server"
    exit 1
fi

# 启动Web服务
echo "🌐 启动Web服务..."
echo "访问地址: http://localhost:8899/"
echo "按 Ctrl+C 停止服务"
echo "=================================="

python3 start_web_service.py
